package com.example.springboot_ruiji.controller;

import com.example.springboot_ruiji.common.R;
import com.example.springboot_ruiji.pojo.Category;
import com.example.springboot_ruiji.pojo.Employee;
import com.example.springboot_ruiji.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R page(Integer page, Integer pageSize){
        pageSize=10;
        return categoryService.page(page,pageSize);
    }
    @PostMapping
    public R add(@RequestBody Category category,HttpSession session){
        Long empId = (Long) session.getAttribute("employee");
        category.setCreateUser(empId);
        category.setUpdateUser(empId);
        return categoryService.add(category);
    }

    @PutMapping
    public R updateStatus(@RequestBody Category category, HttpSession session){
        Long empId = (Long) session.getAttribute("employee");
        category.setCreateUser(empId);
        category.setUpdateUser(empId);
        return categoryService.edit(category);
    }

    @DeleteMapping
    public R del(Long id){
        categoryService.deleteById(id);
        return R.success("删除类别成功");
    }

    @GetMapping("exportExcel")
    public void exportExcel(HttpServletResponse response){
        categoryService.exportExcel(response);
    }
}
